package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.EBook;
import service.impl.EBookServiceImpl;
import util.PageUtil;

public class PageServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
	          this.doPost(request, response);
		out.flush();
		out.close();
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
	
		  String currentPage = request.getParameter("cp");
		  int cp=0;
		  int totalPage=0;
		  EBookServiceImpl esi=new EBookServiceImpl();
		  PageUtil pu=new PageUtil();
		   if(currentPage!=null&&!currentPage.contentEquals("")){
			      cp=Integer.parseInt(currentPage);
		   }
		   
		    if(cp<1){//上一页
		    	  cp=1;
		    }
		    
		  
		    int count=esi.getCount();
		    
		     pu.setTotalCount(count);
		    totalPage= pu.getTotalPage();
		    if(cp>totalPage){//下一页
		    	 cp=totalPage;
		    }
		   
		    
		    pu.setCurrentPage(cp);
		
		     List<EBook> books = esi.ebookByPage(pu);
		    request.getSession().setAttribute("books", books);
		    request.getSession().setAttribute("cp",cp);
		    request.getSession().setAttribute("totalPage", totalPage);
		    
		     response.sendRedirect("index.jsp");
		out.flush();
		out.close();
	}

}
